﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaiTap1
{
    public class BieuThucTong : BieuThuc
    {
        #region - Attribute
        private BieuThuc _bieuThuc1;
        private BieuThuc _bieuThuc2;
        #endregion

        #region - Property
        public BieuThuc BieuThuc1
        {
            get { return _bieuThuc1; }
            set { _bieuThuc1 = value; }
        }
        public BieuThuc BieuThuc2
        {
            get { return _bieuThuc2; }
            set { _bieuThuc2 = value; }
        }
        #endregion

        #region - Method
        public BieuThucTong()
        {
        
        }
        public BieuThucTong(BieuThuc bieuThuc1, BieuThuc bieuThuc2)
        {
            _bieuThuc1 = bieuThuc1;
            _bieuThuc2 = bieuThuc2;
        }
        public override string TinhDaoHam()
        {
            string ketQua = _bieuThuc1.TinhDaoHam();
            string tmp = _bieuThuc2.TinhDaoHam();
            if (ketQua == "")
            {
                if (tmp != "")
                {
                    ketQua = tmp;
                }
            }
            else
            {
                if (tmp != "")
                {
                    ketQua += " + " + tmp;
                }
            }
            if (ketQua.Contains("+"))
                ketQua = "(" + ketQua + ")";
            return ketQua;
        }
        public override double TinhGiaTri(double x)
        {
            return _bieuThuc1.TinhGiaTri(x) + _bieuThuc2.TinhGiaTri(x);
        }
        public void GanBieuThuc(BieuThuc bieuThuc)
        {
            if (_bieuThuc1 == null)
                _bieuThuc1 = bieuThuc;
            else
                _bieuThuc2 = bieuThuc;
        }
        public override string ToString()
        {
            string ketQua = _bieuThuc1.ToString();
            if (ketQua == "")
            {
                if (_bieuThuc2.ToString() != "")
                {
                    ketQua = _bieuThuc2.ToString();
                }
            }
            else
                if (_bieuThuc2.ToString() != "")
                {
                    ketQua += " + " + _bieuThuc2.ToString();
                }
            if (ketQua.Contains("+"))
                ketQua = "(" + ketQua + ")";
            return ketQua;
        }
        #endregion
    }
}
